﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IFServerDLL.Model;

namespace IFClientDLL.Model
{
    public class AccountDemo
    {
        public AccountDemo(AccountBase account)
        {
            _CurrMargin = account.CurrMargin;
            _Available = account.Available;
            _CloseProfit = account.CloseProfit;
            _MarketProfit = account.MarketProfit;
            _OrderFrozenCash = account.OrderFrozenCash;
            _OrderFrozenCommssion = account.OrderFrozenCommssion;
            _TradeOnHandCash = account.TradeOnHandCash;
            _TodayTradeCommssion = account.TodayTradeCommssion;
        }
        private decimal _CurrMargin;
        public decimal CurrMargin
        {
            get { return _CurrMargin; }
        }
        private decimal _Available;
        public decimal Available
        {
            get { return _Available; }
        }

        public decimal Commission
        {
            get { return _OrderFrozenCommssion + _TodayTradeCommssion; }
        }
        private decimal _CloseProfit;
        public decimal CloseProfit
        {
            get { return _CloseProfit; }
        }
        private decimal _MarketProfit;
        public decimal MarketProfit
        {
            get { return _MarketProfit; }
        }
        private decimal _OrderFrozenCash;
        public decimal OrderFrozenCash
        {
            get { return _OrderFrozenCash; }
        }
        private decimal _OrderFrozenCommssion;
        public decimal OrderFrozenCommssion
        {
            get { return _OrderFrozenCommssion; }
        }
        private decimal _TodayTradeCommssion;
        public decimal TodayTradeCommssion { get { return _TodayTradeCommssion; } }
        private decimal _TradeOnHandCash;
        public decimal TradeOnHandCash { get { return _TradeOnHandCash; } }

        public void UpdateMarketProfit(IClient client)
        {
            _MarketProfit = client.TradeOnHand.Sum(p => p.MarketProfit);
            if (OnAccountChanged != null)
            {
                OnAccountChanged(this);
            }
        }
        public void UpdateAll(IClient client)
        {
            _MarketProfit = client.TradeOnHand.Sum(p => p.MarketProfit);
            _CloseProfit = client.TradeGroup.Sum(p => p.SumCloseProfit);
            _OrderFrozenCash = client.OrderHistory.Sum(p => p.FrozenCash);
            _OrderFrozenCommssion = client.OrderHistory.Sum(p => p.FrozenCommission);
            _TradeOnHandCash = client.TradeOnHand.Sum(p => p.SumCash);
            _TodayTradeCommssion = client.TradeHistory.Sum(p => p.Commission);
            _Available = _CurrMargin - Commission - OrderFrozenCash - TradeOnHandCash + CloseProfit;
            if (_Available < 0||OrderFrozenCash<0)
            {
                throw new Exception("available can not less then zero.");
            }
           if (OnAccountChanged != null)
            {
                OnAccountChanged(this);
            }
        }


        public event Action<AccountDemo> OnAccountChanged;
    }
}
